package com.air.repo.jdk8;

public class NotifyDemo1 {
    private static void sleep(long sleepVal){
        try{
            Thread.sleep(sleepVal);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    private static void out(String desc){
        System.out.println(Thread.currentThread().getName() + " : " + desc);
    }
    Object lock = new Object();
    public void startT1(){
        new Thread(() -> {
            synchronized (lock){
                out("1get lock");
                startT2();
                out("2start wait");
                try {
                    lock.wait();
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
                out("6get lock after wait");
                out("7release lock");
            }
        }, "t1").start();
    }
    public void startT2(){
        new Thread(()->{
            synchronized (lock){
                out("3get lock");
                startT3();
                sleep(100);
                out("4start notify");
                lock.notify();
                sleep(200);
                out("5release lock");
            }
        },"t2").start();
    }
    public void startT3(){
        new Thread(() -> {
            synchronized (lock){
                out("8get lock");
                out("9release lock");
            }
        }, "t3").start();
    }
    public static void main(String[] args){
        new NotifyDemo1().startT1();
    }
}